package hpp.test;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hpp on 2017/4/16.
 */
public class ConvertToNum {

    private static final long WAN = 10000L;
    private static final long YI = 100000000L;
    private static final long WANYI = 1000000000000L;

    private static Integer getInteger(String s) {
        Map<String, Integer> map = new HashMap<>();
        map.put("个", 1);
        map.put("十", 10);
        map.put("百", 100);
        map.put("千", 1000);
        map.put("万", 10000);
        map.put("亿", 100000000);
        if (map.containsKey(s)) {
            return map.get(s);
        }
        return 1;
    }

    private static Integer getNum(String s) {
        Map<String, Integer> map = new HashMap<>();
        map.put("一", 1);
        map.put("二", 2);
        map.put("两", 2);
        map.put("三", 3);
        map.put("四", 4);
        map.put("五", 5);
        map.put("六", 6);
        map.put("七", 7);
        map.put("八", 8);
        map.put("九", 9);
        if (map.containsKey(s)) {
            return map.get(s);
        }
        return 1;
    }

    //最多支持到千万亿量级
    public static long ConvertToNumMethod(String s) throws Exception {
        if (s.contains("兆")) {
            return 0;//TODO:不支持兆单位以上转换
        }
        long num = 0;
        if (s.contains("亿")) {//TODO:兆元以下量级
            String[] splitY = s.split("亿");
            if (splitY.length > 1) {
                if (splitY[0].contains("万")) {
                    String[] splitW = splitY[0].split("万");
                    if (splitW.length > 1) {
                        num = convertChineseToNum(splitW[0]) * WANYI + convertChineseToNum(splitW[1]) * YI;
                    } else {
                        num = convertChineseToNum(splitW[0]) * WANYI;
                    }
                } else {
                    num = convertChineseToNum(splitY[0]) * YI;
                }

                if (splitY[1].contains("万")) {//TODO:亿元以下量级
                    String[] splitW = splitY[1].split("万");
                    if (splitW.length > 1) {
                        num += convertChineseToNum(splitW[0]) * WAN + convertChineseToNum(splitW[1]);
                    } else {
                        num += convertChineseToNum(splitW[0]) * WAN;
                    }
                } else {
                    num += convertChineseToNum(s);
                }
                return num;
            } else {//TODO:最后一个单位是亿元
                if (splitY[0].contains("万")) {
                    String[] splitW = splitY[0].split("万");
                    if (splitW.length > 1) {
                        return convertChineseToNum(splitW[0]) * WANYI + convertChineseToNum(splitW[1]) * YI;
                    } else {
                        return convertChineseToNum(splitW[0]) * WANYI;
                    }
                } else {
                    return convertChineseToNum(splitY[0]) * YI;
                }
            }

        } else if (s.contains("万")) {//TODO:亿元以下量级
            String[] splitW = s.split("万");
            if (splitW.length > 1) {
                return convertChineseToNum(splitW[0]) * WAN + convertChineseToNum(splitW[1]);
            } else {
                return convertChineseToNum(splitW[0]) * WAN;
            }
        } else {
            return convertChineseToNum(s);
        }
    }

    //TODO:转换万位以下的中文金额到数字，例如："三千两百二十一" to "3221"
    public static long convertChineseToNum(String str) {
        long num = 0;
        str = str.replace("零", "");
        char[] b = (str + "个").toCharArray();
        for (int i = 0; i <= b.length - 2; i = i + 2) {
            num += getNum(String.valueOf(b[i])) * getInteger(String.valueOf(b[i + 1]));
        }
        return num;
    }


    public static void main(String[] args) {

        String a = "两千万三百八十亿四千两百五十一万六千零一十九";
        String b = "二万五千三百零三亿";
        String c = "三千四百五十一万六千零一十九";
        String d = "三万零一百";
        String e = "六千零一十九";
        String f = "一";
        String g = "一兆";
        String h = "五万三千";

        try {

            System.out.println(ConvertToNumMethod(a));
            System.out.println(ConvertToNumMethod(b));
            System.out.println(ConvertToNumMethod(c));
            System.out.println(ConvertToNumMethod(d));
            System.out.println(ConvertToNumMethod(e));
            System.out.println(ConvertToNumMethod(f));
            System.out.println(ConvertToNumMethod(g));
            System.out.println(ConvertToNumMethod(h));

        } catch (Exception exception) {
            exception.printStackTrace();
        }

    }
}
